<?php
/*
// JoomlaWorks "Tabs & Slides" Plugin for Joomla! 1.0.x - Version 2.3
// License: http://www.gnu.org/copyleft/gpl.html
// Authors: Fotis Evangelou - George Chouliaras
// Copyright (c) 2006 - 2007 JoomlaWorks.gr - http://www.joomlaworks.gr
// Project page at http://www.joomlaworks.gr - Demos at http://demo.joomlaworks.gr
// Support forum at http://forum.joomlaworks.gr
// ***Last update: August 30th, 2007***
*/

/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onPrepareContent', 'jwTabs' );

function jwTabs( $published, &$row, &$params, $page=0  ) {
	
// JS loader selection
$use_optimized_loader = 0; // Use optimized JS code loader? 0=no and 1=yes. Default is 0.

  if (!$published) {
    if (preg_match_all("/{tab=.+?}/", $row->text, $matches, PREG_PATTERN_ORDER) > 0) {
     foreach ($matches[0] as $match) {
      $match = str_replace("{tab=", "", $match);
      $match = str_replace("}", "", $match);
      $row->text = str_replace( "{tab=".$match."}", "", $row->text );
      $row->text = str_replace( "{/tabs}", "", $row->text );
     }     
    }
    if (preg_match_all("/{slide=.+?}/", $row->text, $matches, PREG_PATTERN_ORDER) > 0) {
      foreach ($matches[0] as $match) {
        $match = str_replace("{slide=", "", $match);
        $match = str_replace("}", "", $match);
        $row->text = str_replace( "{slide=".$match."}", "", $row->text );
        $row->text = str_replace( "{/slide}", "", $row->text );
      } 
    }  	
    return;
  }

  global $mosConfig_absolute_path, $mosConfig_live_site, $mosConfig_session_type, $mainframe, $database, $loadJWTSscripts;
  static $tabid; 
  $query = "SELECT id FROM #__mambots WHERE element = 'plugin_jw_ts' AND folder = 'content'";
  $database->setQuery( $query );
  $id = $database->loadResult();
  $mambot = new mosMambot( $database );
  $mambot->load( $id );
  $param =& new mosParameters( $mambot->params );  
  // parameters
  $enable_tabs = $param->get('enable_tabs', 1);
  $enable_slides = $param->get('enable_slides', 1);
  $slides_slidespeed = $param->get('slides_slidespeed', '30');
  $slides_timer = $param->get('slides_timer', '10');

  if(!$loadJWTSscripts) {
	$loadJWTSscripts=1;
	$header = "
<!-- JoomlaWorks \"Tabs & Slides\" Plugin (v2.3) starts here -->		
<style type=\"text/css\" media=\"screen\">
	@import \"$mosConfig_live_site/mambots/content/plugin_jw_ts/tabs_slides.css\";
</style>
<style type=\"text/css\" media=\"print\">.jwts_tabbernav{display:none;}</style>
<script type=\"text/javascript\">var jwts_slideSpeed=".$slides_slidespeed."; var jwts_timer=".$slides_timer.";</script>
<script type=\"text/javascript\" src=\"$mosConfig_live_site/mambots/content/plugin_jw_ts/tabs_slides_comp.js\"></script>
";
	if($use_optimized_loader) {
		$header .= "<script type=\"text/javascript\" src=\"$mosConfig_live_site/mambots/content/plugin_jw_ts/tabs_slides_opt_loader.js\"></script>";
	} else {
		$header .= "<script type=\"text/javascript\" src=\"$mosConfig_live_site/mambots/content/plugin_jw_ts/tabs_slides_def_loader.js\"></script>";
	}
	$header .= "
<!-- JoomlaWorks \"Tabs & Slides\" Plugin (v2.3) ends here -->
";
	// cache check
	if($mainframe->getCfg('caching') && ($option=='com_frontpage' || $option=='')) {
		echo $header;
	} else {
		$mainframe->addCustomHeadTag($header);
	}
}
  
  // Start Tabs Replacement
  // index.php
  if($enable_tabs) {
  if(strpos($_SERVER['PHP_SELF'], "index.php")) {
   $b=1;
   if (preg_match_all("/{tab=.+?}{tab=.+?}|{tab=.+?}|{\/tabs}/", $row->text, $matches, PREG_PATTERN_ORDER) > 0) { 	
    foreach ($matches[0] as $match) {	
      if($b==1 && $match!="{/tabs}") {
    	$tabs[] = 1;
    	$b=2;
      }
      elseif($match=="{/tabs}"){
      	$tabs[]=3;
      	$b=1;
      }
      elseif(preg_match("/{tab=.+?}{tab=.+?}/", $match)){
      	$tabs[]=2;
      	$tabs[]=1;
      	$b=2;
      }
      else {
      	$tabs[]=2;
      }
    }
   }
   @reset($tabs);
   $tabscount = 0;
  if (preg_match_all("/{tab=.+?}|{\/tabs}/", $row->text, $matches, PREG_PATTERN_ORDER) > 0) {
    foreach ($matches[0] as $match) {
      if($tabs[$tabscount]==1) {
      	$match = str_replace("{tab=", "", $match);
        $match = str_replace("}", "", $match);
        $row->text = str_replace( "{tab=".$match."}", "
		<div class=\"jwts_tabber\" id=\"jwts_tab".$tabid."\"><div class=\"jwts_tabbertab\" title=\"".$match."\"><h2><a href=\"javascript:void(null);\" name=\"advtab\">".$match."</a></h2>", $row->text );        
        $tabid++;
      } elseif($tabs[$tabscount]==2) {
      	$match = str_replace("{tab=", "", $match);
        $match = str_replace("}", "", $match);
      	$row->text = str_replace( "{tab=".$match."}", "</div><div class=\"jwts_tabbertab\" title=\"".$match."\"><h2><a href=\"javascript:void(null);\" name=\"advtab\">".$match."</a></h2>", $row->text );
      } elseif($tabs[$tabscount]==3) {
      	$row->text = str_replace( "{/tabs}", "</div></div><div class=\"jwts_clr\"></div>", $row->text );
      }
      $tabscount++;
    }     
  }    	
  // index2.php
  } else {
   if (preg_match_all("/{tab=.+?}/", $row->text, $matches, PREG_PATTERN_ORDER) > 0) {
    foreach ($matches[0] as $match) {
      $match = str_replace("{tab=", "", $match);
      $match = str_replace("}", "", $match);
      $row->text = str_replace( "{tab=".$match."}", "</div><div><h3>".$match."</h3>", $row->text );
      $row->text = str_replace( "{/tabs}", "", $row->text );
    }     
   }   	
  }
  }
  // End Tabs Replacement
  
  // Start Slides Replacement
  // index.php
  if($enable_slides) {
  if(strpos($_SERVER['PHP_SELF'], "index.php")) {
   if (preg_match_all("/{slide=.+?}/", $row->text, $matches, PREG_PATTERN_ORDER) > 0) {
    foreach ($matches[0] as $match) {
      $match = str_replace("{slide=", "", $match);
      $match = str_replace("}", "", $match);
      $row->text = str_replace( "{slide=".$match."}", "<div class=\"jwts_title\"><div class=\"jwts_title_left\"><a href=\"javascript:void(null);\" title=\"Click to open!\" class=\"jwts_title_text\">".$match."</a></div></div><div class=\"jwts_slidewrapper\"><div>", $row->text );
      $row->text = str_replace( "{/slide}", "</div></div>", $row->text );
    }   
   }
  // index2.php 
  } else {
  if (preg_match_all("/{slide=.+?}/", $row->text, $matches, PREG_PATTERN_ORDER) > 0) {
    foreach ($matches[0] as $match) {
      $match = str_replace("{slide=", "", $match);
      $match = str_replace("}", "", $match);
      $row->text = str_replace( "{slide=".$match."}", "<h3>&nbsp;".$match."</h3><div>", $row->text );
      $row->text = str_replace( "{/slide}", "</div>", $row->text );
    } 
   }  	
  }
  }
  // End Slides Replacement
}

?>
